#资源model add by Luxh
common_util = require('../services/common_util')
config = require('../config')
Schema = config.Schema
mongoose = config.mongoose

SysResourceSchema = new Schema({
  res_code:
    type:String
    index:true
  res_name:String
  res_parent:String
  url:String
  http_method:String
  status:Number
  type:String
  order:Number
})

exports.SysResoure = mongoose.model 'sys_resource',SysResourceSchema
SysResource = mongoose.model 'sys_resource'

#保存
exports.save = (req,callback) ->
  sysResource = new SysResource({
    res_code:common_util.trim(req.param("res_code"))
    res_name:req.param("res_name")
    res_parent:req.param("parent_id")
    http_method:req.param("http_method")
    url:req.param("url")
    status:req.param("status")
    type:req.param("type")
    order:req.param("order")
  })
  sysResource.save (err) ->
    if(err)
      callback(err)
    else
      callback(null)

#根据资源编码判断资源是否已存在
exports.isResourceExist = (res_code,callback) ->
  SysResource.count {res_code:res_code},(err,count) ->
    if err
      callback(err)
    else
      callback(null,count)